package main

import (
	"gitee.com/ameise84/e3log"
	"github.com/fatih/color"
	jsoniter "github.com/json-iterator/go"
	"golang.org/x/time/rate"
	"os"
)

type X1 struct {
	Name string `json:"name"`
}

func (ts *X1) E3LogMarshall() string {
	out, _ := jsoniter.MarshalToString(ts)
	return out
}

type X2 struct {
	Name string `json:"name"`
}

func (ts *X2) E3LogMarshall() string {
	return ts.Name
}

func T1() {
	w1 := e3log.NewConsoleWriter(os.Stdout)
	w1.SetEasyRead(true)
	w1.SetNoColor(false)
	cfg := e3log.DefaultFileWriterConfig()
	cfg.FileName = "test_log/app.log"
	w2, _ := e3log.NewFileWriter(cfg)
	e3log.SetLogKeyName(e3log.LogKeyMsg, "msg")
	log := e3log.NewLogger(w1, w2).SetLevel(e3log.LevelTrace).SetOutCaller(true).SetOutErrStack(false).SetPrefix("test6")
	log.SetLimiter(e3log.NewLimiterBase(rate.NewLimiter(1, 10)))
	log.Info("<test1>").Str("k1", "1\t\t").Int("k2", 1).Println()
	log.Error("test2", true).Str("<k3>", "2\t\t").Int("k4", 2).Println()
	log.ColorMsg(e3log.LevelTrace, color.RGB(32, 100, 100), "tes3").Println()
	e3log.Sugar(log)
	e3log.Msg(e3log.LevelWarn, "sugar msg warn").Println()
	e3log.ColorMsg(e3log.LevelWarn, color.RGB(32, 100, 100), "sugar color msg warn").Println()
	e3log.Trace("sugar trace")

	x1 := X1{"wo shi x1"}
	x2 := X2{"wo shi x2"}

	x12 := X1{"中<wo shi 中国人>"}
	x22 := X2{"[wo shi x22]"}
	xx := []e3log.LogMarshaller{&x1, &x12}
	log.Info("obj").Objects(&x1, &x2, &x12, &x22).Str("test12", x12.Name).Err(nil).Println()
	log.Info("obj").Objects(xx...).Err(nil).Println()
	log.Wait()
}

func main() {
	T1()
}
